package com.controller.back;

import com.entity.User;
import com.github.pagehelper.PageInfo;
import com.service.UserService;
import com.util.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("back/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public String login(String email, String password, HttpServletResponse res, HttpSession session){
        Cookie c = new Cookie("name",null);
        res.addCookie(c);
        boolean login = userService.login(email, password);
        if(login){
            session.setAttribute("login",true);
            return "redirect:/front/main.jsp";
        }else{
            return "redirect:/front/login.jsp";
        }
    }
    @RequestMapping("/register")
    public String register(String email, String password, String salt, String nickname, Integer status, String code, Date create_date,String VCode,HttpSession session){
        String realCode = (String) session.getAttribute("realCode");
        if (VCode != null && !VCode.equalsIgnoreCase(realCode)) {
            //如果验证码不正确，重定向到登录页面
            return "redirect:/front/login.jsp";
        }
        boolean register = userService.register(email, password, salt, nickname, status, code, create_date);
        if(register){
            return "redirect:/front/login.jsp";
        }else{
            return "redirect:/front/register.jsp";
        }
    }
    @RequestMapping("add")
    public String addUser(@ModelAttribute("u") @Validated User user, BindingResult bindingResult, Model model){
        if(ValidatorUtils.handleBindingResult(bindingResult,model)){
            return "forward:/back/user/add.jsp";
        }
        userService.insertUser(user);
        return "redirect:/back/user/show.do";
    }
    @RequestMapping("batchRemove")
    public String batchRemoveUser(@RequestParam("ids") List<Integer> ids){
        for(Integer id:ids){
            userService.deleteUserById(id);
        }
        return "redirect:/back/user/show.do";
    }
    @RequestMapping("remove")
    public String removeUser(Integer id){
        userService.deleteUserById(id);
        return "redirect:/back/user/show.do";
    }
    @RequestMapping("show")
    public String showAllUser(@RequestParam(defaultValue = "1")Integer pageNum,
                              @RequestParam(defaultValue = "3")Integer pageSize,
                              @RequestParam(defaultValue = "")String query,
                              @RequestParam(defaultValue = "")String value,
                              Model model){
        PageInfo<User> users = userService.selectPageUsers(pageNum,pageSize,query,value);
        model.addAttribute("pageInfo",users);
        model.addAttribute("query",query);
        model.addAttribute("value",value);
        return "forward:/back/user/show.jsp";
    }
    @RequestMapping("showOne")
    public String showOneUser(Integer id,HttpServletRequest req){
        User user = userService.selectUserById(id);
        req.setAttribute("user",user);
        return "forward:/back/user/update.jsp";
    }
    @RequestMapping("updateStatus")
    public String updateUser(@Validated User user, BindingResult bindingResult, Model model){
        if(ValidatorUtils.handleBindingResult(bindingResult,model)){
            return "forward:/back/user/update.jsp";
        }
        userService.updateUser(user);
        return "redirect:/back/user/show.do";
    }
}
